﻿function maildetail(mailid) {
    $(".fr-maildetail").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: '/EmailTicket/EmailDetails',
        data: { id: mailid },
        success: function (data) {
            $(".fr-maildetail").html(data);
        }
    });
}

function ticketdetail(ticketid) {
    $(".fr-ticketdetail").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: '/EmailTicket/TicketDetails',
        data: { ticketId: ticketid },
        success: function (data) {
            $(".fr-ticketdetail").html(data);
        }
    });
}

function ViewReplyEmail(id) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/ReplyEmail",
        data: { id: id },
        success: function (data) {
            $("#ViewReplyEmail").html(data);
        },
        error: function () {
            $("#ViewReplyEmail").html("Load failed");
        }
    });
}

function ViewReplyEmailFull(id) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/ReplyEmailFull",
        data: { id: id },
        success: function (data) {
            $("#ReplyEmailFull").html(data);
        },
        error: function () {
            $("#ReplyEmailFull").html("Load failed");
        }
    });
}
function CreateTicket(id) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/CreateTicket",
        data: { id: id },
        success: function (data) {
            $("#CreateTicket").html(data);
            initfrReplyEmail();
        },
        error: function () {
            $("#CreateTicket").html("Load failed");
        }
    });
}
function LinkToTicket(id) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/LinkToTicket",
        data: { id: id },
        success: function (data) {
            $("#LinkToTicket").html(data);
            LoadDataTable($("#LinkToTicketDTTB"));
        },
        error: function () {
            $("#LinkToTicket").html("Load failed");
        }
    });
}

function EditTicket(id) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/EditTicket",
        data: { id: id },
        success: function (data) {
            $("#EditTicket").html(data);
            initfrReplyEmail();
        },
        error: function () {
            $("#EditTicket").html("Load failed");
        }
    });
}

function initfrReplyEmail() {
    $("#datetimepicker1").datepicker({
        rtl: App.isRTL(),
        autoclose: true
    });
}

function showFileName() {
    var fil = document.getElementById("InputFile");   
    alert(fil.value);
}
function SetTextAreaValue(value) {
    $('#txtContent').html(value);
}

function FilterEmail() {
    $('#frFilterEmail').submit();
}


function FilterTicket() {
    $('#frFilterTicket').submit();
}

function ticketrelationdetail(id) {
    
    $("#DivTicketRelationDetail").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: '/EmailTicket/TicketRelationDetail',
        data: { id: id },
        success: function (data) {
            $("#DivTicketRelationDetail").html(data);
        },
        error: function () {
            $("#DivTicketRelationDetail").html("Load failed");
    }
    });
}

function newticketrelationdetail() {
    $('#txtValue').val("");
    $('#iRelationID').val(null);
}

function ViewRelationObject(TaskId) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/ViewRelationObject",
        data: { TaskId: TaskId},
        success: function (data) {
            $("#DivViewRelationObject").html(data);
        },
        error: function () {
            $("#DivViewRelationObject").html("Load failed");
        }
    });
}

function ViewAudit(ticketId) {
    //App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/EmailTicket/ViewAudit",
        data: { ticketId: ticketId },
        success: function (data) {
            $("#DivViewAudit").html(data);
        },
        error: function () {
            $("#DivViewAudit").html("Load failed");
        }
    });
}
function checkaddtaskrequirederr(selector) {
    selector.removeClass("addtask-required-warring");
    var err = false;
    if (selector.val() == 0) {
        selector.addClass("addtask-required-warring");
        err = true;
    }
    return err;
}
function initfrAddTaskSubmit() {
    $('#frAddTask').submit(function () {
        App.blockUI($("#frAddTask"));
            var err = false;
            err = checkaddtaskrequirederr($("#TaskTypeID")) || err;
            err = checkaddtaskrequirederr($("#TaskTitleID")) || err;
            err = checkaddtaskrequirederr($("#ReasonID")) || err;
            //err = checkaddtaskrequirederr($("#CompleteByHrs")) || err;
            err = checkaddtaskrequirederr($("#PriorityID")) || err;
            err = checkaddtaskrequirederr($("#AssignedToUser")) || err;
            $("#TaskDescription").removeClass("addtask-required-warring");
            if ($("#TaskDescription").val() == "") {
                $("#TaskDescription").addClass("addtask-required-warring");
                err = true;
            }

        if (err) {
            App.unblockUI($("#frAddTask"));
            alert("Some field has not been filled. Check and fill it, please!");
        } else {
            var submitData = $(this).serialize();
            $.ajax({
                    type: "POST",
                    url: '/EmailTicket/CheckOrderRef',
                    data: { orderref: $("#OrderRef").val() },
                    dataType: "text",
                    success: function(data) {
                        if (data == 'True') {
                            $.ajax({
                                type: 'POST',
                                url: '/EmailTicket/AddTask',
                                cache: false,
                                data: submitData,
                                success: function(data) {
                                    App.unblockUI($(".modal"));
                                    window.location = window.location;
                                }
                            });
                            SetGroupOrderRefStatus(true);
                        } else {
                            App.unblockUI($("#frAddTask"));
                            SetGroupOrderRefStatus(false);
                            $("#OrderRef").focus();
                            $("#icoGroupOrderRef").tooltip('destroy');
                            $("#icoGroupOrderRef").tooltip('show');
                        }
                    }
                });
            
        }
        return false;
    });
}
function SetGroupOrderRefStatus(exist) {
    if (exist) {
        $("#groupOrderRef").addClass("has-success");
        $("#groupOrderRef").removeClass("has-error");
        $("#icoGroupOrderRef").removeClass("icon-exclamation-sign");
        $("#icoGroupOrderRef").addClass("icon-ok");
        $("#icoGroupOrderRef").attr('title', 'OrderRef exist');
        $("#icoGroupOrderRef").tooltip('destroy');
        $("#icoGroupOrderRef").tooltip();
    } else {
        $("#groupOrderRef").addClass("has-error");
        $("#groupOrderRef").removeClass("has-success");
        $("#icoGroupOrderRef").addClass("icon-exclamation-sign");
        $("#icoGroupOrderRef").removeClass("icon-ok");
        $("#icoGroupOrderRef").attr('title', 'OrderRef incorrect');
        $("#icoGroupOrderRef").tooltip('destroy');
        $("#icoGroupOrderRef").tooltip();
    }
}
function CheckOrderRefExist() {
    $.ajax({
        type: "POST",
        url: '/EmailTicket/CheckOrderRef',
        data: { orderref: $("#OrderRef").val() },
        dataType: "text",
        success: function(data) {
            if (data == 'True') {
                SetGroupOrderRefStatus(true);
                return true;
            } else {
                SetGroupOrderRefStatus(false);
                return false;
            }
        }
    });
}

function ClearAlert() {
    $('#msg-frAddTask').html("");
}

function AddMoreAttachment() {
    var countAttachment = $("#CountAttachment").val() + 1;
    $.ajax({        
        url: "/EmailTicket/AddMoreAttachment",
        data: { CountAttachment: countAttachment },
        success: function (data) {
            $("#divMoreAttachment").append(data);            
            $("#CountAttachment").val(countAttachment);
        },
        error: function () {
            $("#divMoreAttachment").html("Load failed");
        }
    });
}

function CreateTask(emailId) {
    App.blockUI($(".page-content"));
    $.ajax({
        url: '/EmailTicket/AddTask',
        methid: "GET",
        data: { emailId: emailId },
        success: function(data) {
            $("#CreateTaskModal").html(data);
            $("#datetimepicker1").datetimepicker({
                autoclose: true,
                isRTL: App.isRTL(),
                format: "mm/dd/yyyy hh:ii",
                pickerPosition: ("top-left")
            });
            App.unblockUI($(".page-content"));
            $("#CreateTaskModal").modal();
            CheckOrderRefExist();
            $("#icoGroupOrderRef").tooltip('destroy');
            initfrAddTaskSubmit();
            $("#OrderRef").keyup(function () { CheckOrderRefExist(); });
        },
        error: function() {
            App.unblockUI($(".page-content"));
        }
    });
}

function gettitles(taskTypeId) {
    $.ajax({
        url: "/task/gettitles",
        data: { taskTypeId: taskTypeId },
        success: function (data) {
            $("#TaskTitleID").html('');
            $.each(data, function (id, option) {
                $("#TaskTitleID").append($('<option></option>').val(option.id).html(option.name));
            });
        }
    }
    );
}

function ViewTask(taskId) {
    $.ajax({
        url: "/EmailTicket/GetOrderRefFromTaskId",
        data: { taskId: taskId },
        success: function (data) {
            if (data != "") {
                PopupCenter('/orderview/viewtask/' + taskId + '?orderref=' + data, 600, 540);
            } else {
                alert("Error!");
            }
        }
    }
    );
}

